import datetime

from django import forms
from django.forms import TextInput

from martor.fields import MartorFormField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column
from martor.widgets import MartorWidget

from .models import LabLog, ShiftSchedule


class LabLogModelForm(forms.ModelForm):
    class Meta:
        model = LabLog
        fields = ('weather', 'log_time', 'lab_template', 'lab_humidity',
                  'hc_room_template', 'hc_room_humidity', 'network_delay',
                  'packet_lose_rate', 'description')

    def clean(self):
        cleaned_data = super(LabLogModelForm, self).clean()
        log_time = cleaned_data.get('log_time')
        print(log_time)

    def __init__(self, *args, **kwargs):
        self.log_date = kwargs.pop('log_date')
        self.time_slot = kwargs.pop('time_slot')
        super(LabLogModelForm, self).__init__(*args, **kwargs)

        if self.time_slot == 1:
            self.fields['log_time'].initial = datetime.time(9, 0, 0)
        elif self.time_slot == 2:
            self.fields['log_time'].initial = datetime.time(21, 0, 0)

    # def __init__(self, *args, **kwargs):
    #     self.create_or_update = kwargs.pop('create_or_update')
    #     super(LabLogModelForm, self).__init__(*args, **kwargs)
    #
    #     submit_value = '保存'
    #     if self.create_or_update == 1:
    #         submit_value = '添加'
    #     elif self.create_or_update == 2:
    #         submit_value = '更新'
    #
    #     self.helper = FormHelper()
    #     self.helper.layout = Layout(
    #         Row(
    #             Column('weather', css_class='form-group col-md-2 mb-0'),
    #             Column('log_time', css_class='form-group col-md-2 mb-0'),
    #             css_class='form-row'
    #         ),
    #         Row(
    #             Column('lab_template', css_class='form-group col-md-2 mb-0'),
    #             Column('lab_humidity', css_class='form-group col-md-2 mb-0'),
    #             css_class='form-row'
    #         ),
    #         Row(
    #             Column('hc_room_template', css_class='form-group col-md-2 mb-0'),
    #             Column('hc_room_humidity', css_class='form-group col-md-2 mb-0'),
    #             css_class='form-row'
    #         ),
    #         Row(
    #             Column('network_delay', css_class='form-group col-md-2 mb-0'),
    #             Column('packet_lose_rate', css_class='form-group col-md-2 mb-0'),
    #             css_class='form-row'
    #         ),
    #         'description',
    #         Submit('submit', submit_value)
    #     )


class ShiftScheduleModelForm(forms.ModelForm):
    class Meta:
        model = ShiftSchedule
        fields = ('duty_date', 'arrange_duty_person',)
        widgets = {'duty_date': forms.TextInput(attrs={'readonly': True}),
                   'arrange_duty_person': forms.Select(), }
